<?php namespace web\home\controller;
		class Show extends Common{
			public function index(){
				$gid=q('get.gid',0,'intval');
				//获得商品详情数据
					$a=Db::table('goods')
						->join('detail','goods.gid','=','detail.gid')
						->get();

					foreach($a as $k=>$v){
						if($v['gid']==$gid){
							$goods=$v;
						}
					}
				$goods['small']=explode(',',$goods['small']);
				$goods['medium']=explode(',',$goods['medium']);
				$goods['big']=explode(',',$goods['big']);
//				p($goods);
				$cid=$goods['cid'];
				$tid=$goods['tid'];
				$categoryData=Db::table('category')->get();
				//面包屑
				$Fathercids=$this->getFather($categoryData,$cid);
				$fatherData=array_reverse(Db::table('category')->whereIn('cid',$Fathercids)->get());
				View::with('fatherData',$fatherData);
				//获得规格值
				$specData=Db::table('type_attr')->where('tid',$tid)->where('class',2)->get();
					foreach($specData as $k=>$v){
						$specData[$k]['spec']=Db::table('goods_attr')->where('taid',$v['taid'])->where('gid',$gid)->get();
					}
//				p($specData);
				//获得属性值
				$attrData=Db::table('type_attr')->where('tid',$tid)->where('class',1)->get();
				foreach($attrData as $k=>$v){
					$attrData[$k]['attr']=Db::table('goods_attr')->where('taid',$v['taid'])->where('gid',$gid)->get();
				}

//				分配title数据
				$conf=['title'=>$goods['gname']];
//				分配js
				$js=['nav'=>"{{__ROOT__}}/resource/home/js/nav.js"];
				View::with('js',$js);

				//		获得热门推荐
				$hotGoods=Db::table('goods')->where('cid',$cid)->limit(8)->get();
				//获得热销榜单数据
				$hotXiao=Db::table('goods')->orderBy('click','desc')->limit(6)->get();
				View::with('hotGoods',$hotGoods)->with('hotXiao',$hotXiao);
				View::with('goods',$goods)->with('attrData',$attrData)->with('specData',$specData)->with('conf',$conf)->make();
			}
//			异步获取库存
			public function ajaxGetTotal(){
//				p($_POST);
				$data=$_POST;
				//根据组合id和商品id在货品列表数据
				$data['combine']=rtrim($data['combine'],'|');

				$data=Db::table('goods_list')->where('combine',$data['combine'])->where('gid',$data['gid'])->first();
				//如果找不到数据
				if(!$data){echo json_encode(['valid'=>0]);exit;}
				//如果库存为0
				if($data['inventory']==0){echo json_encode(['valid'=>0]);exit;}
				//能走到这里说明数据存在且库存不为0
				echo json_encode(['valid'=>1,'total'=>$data['inventory']]);exit;
			}
	//添加购物车
			public function ajaxAddCart(){
				$data=$_POST;
				//获取单一数据
				$onGoods=Db::table('goods')->where('gid',$data['gid'])->first();
				$data = [
					'id'        => $data['gid'], // 商品 ID
					'name'      =>$onGoods['gname'],// 商品名称
					'num'       => $data['num'], // 商品数量
					'price'     => $onGoods['shopprice'], // 商品价格
					'kucun'     =>$data['kucun'],
					'options'   => $data['options'],// 其他参数如价格、颜色、可以为数组或字符串
				];
				Cart::add($data);
				if($data){
					echo 1;
				}
			}
	//递归找父级
			public function getFather($data,$cid){
				static $temp=[];
				foreach($data as $key=>$value){
					if($cid==$value['cid']){
						$temp[]=$value['cid'];
						$this->getFather($data,$value['pid']);
					}
				}
				return $temp;
			}
		}